<!--
 * @Author: ChaiHongJun
 * @Date: 2021-03-23 17:37:35
 * @LastEditors: ChaiHongJun
 * @LastEditTime: 2021-05-03 14:29:42
 * @version: 
 * @Description: 推荐新歌组件
-->
<template>
  <div class="recommend-music">
    <h2 class="title">推荐新歌</h2>
    <div class="list" v-loading="loadingRecommend">
      <div
        class="item"
        v-for="(newSong, index) of getNewSongs"
        :key="newSong.id"
      >
        <div class="wrapper flex-center shadow">
          <div class="index-container flex-center">
            <span class="num">{{ index + 1 }}</span>
            <div class="play-icon"></div>
            <i
              class="iconfont nicebofang2 play-btn"
              @click="playSingleTrack(newSong)"
            ></i>
            <!-- <i
              class="iconfont nicezanting1 pause-btn"
              @click="pauseTrack(newSong)"
            ></i> -->
          </div>
          <div class="avatar">
            <el-image :src="newSong.picUrl + '?param=150y150'" lazy></el-image>
            <div class="layer flex-center">
              <i class="iconfont niceicon-9"></i>
            </div>
          </div>
          <div class="info">
            <p class="name ellipsis">{{ newSong.name }}</p>
            <p class="flex-row ellipsis">
              <span>{{ newSong.song.artists[0].name }}</span>
            </p>
          </div>
          <p class="album">《{{ newSong.song.album.name }}》</p>
          <p class="duration transition">{{ newSong.song.duration }}</p>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { mapGetters } from "vuex";
import { getTrackSrc } from "@/utils/index.js";
export default {
  data() {
    return {
      loadingRecommend: false,
      limit: 8,
    };
  },
  mounted() {
    this.loadingRecommend = true;
    this.$store.dispatch("getNewSongs", this.limit);
    this.loadingRecommend = false;
  },
  computed: {
    ...mapGetters(["getNewSongs", "getPlayingQueue"]),
  },
  methods: {
    //播放歌曲
    playSingleTrack(newSong) {
      //添加播放歌曲的SRC
      newSong.audioSrc = getTrackSrc(newSong.id);
      this.$store.dispatch("playSingleTrack", newSong);
      //显示播放器
      this.$store.dispatch("showPlayer", true);
      this.$store.commit("PLAY_OR_PAUSE", true);
      // this.$refs.audioPlayer.play();
      // this.$nextTick(() => {
      //   // this.$refs.audioPlayer.play();
      //   console.log(this.$refs.audioPlayer);
      // });
      console.log("播放歌曲：");
      console.log(newSong);
    },
    pauseTrack() {},
  },
};
</script>
<style lang="stylus" scoped>
.recommend-music {
  .title {
    margin: 0 0 15px 0;
  }
}

.list {
  display: flex;
  flex-wrap: wrap;
  margin: 0 -15px;

  .item {
    flex: 0 0 50%;
    max-width: 50%;
    margin-bottom: 20px;
    height: 80px;
    padding: 0 15px 30px;

    .wrapper {
      width: 100%;
      height: 80px;
      background-color: #ffffff;
      justify-content: start;
      padding: 0 4%;
      border-radius: 5px;
      position: relative;

      .bg {
        width: 100%;
        height: 80%;
        position: absolute;
        background-repeat: no-repeat;
        background-size: cover;
        top: 50%;
        left: 0;
        margin: -32px 0 0 0;
        opacity: 0.08;
        filter: blur(10px);
        z-index: -1;
      }

      .index-container {
        width: 30px;
        margin-right: 20px;

        .num {
          font-size: 15px;
          color: #4a4a4a;
          font-weight: bold;
        }

        .play-icon {
          display: none;
          height: 16px;
          min-width: 18px;
          overflow: hidden;

          .line {
            width: 2px;
            height: 100%;
            margin-left: 2px;
            background-color: #ff410f;
            animation: play 0.9s linear infinite alternate;
          }
        }

        .play-btn {
          color: $color-theme;
          font-size: 28px;
          display: none;
          text-align: left;
          cursor: pointer;
        }

        .pause-btn {
          color: $color-theme;
          font-size: 30px;
          display: none;
          text-align: left;
          cursor: pointer;
        }
      }

      .avatar {
        width: 55px;
        height: 55px;
        border-radius: 5px;
        position: relative;
        margin-right: 30px;

        img {
          width: 100%;
          height: 100%;
          border-radius: 5px;
        }

        .layer {
          width: 100%;
          height: 100%;
          position: absolute;
          top: 0;
          left: 0;
          background: rgba(0, 0, 0, 0.6);
          border-radius: 5px;
          opacity: 0;

          i {
            color: #ffffff;
            font-size: 26px;
            font-weight: 100;
          }
        }
      }

      .info {
        width: 15%;

        .name {
          font-size: 14px;
          color: #333333;
          font-weight: bold;
          margin-bottom: 10px;
        }

        .author {
          font-size: 12px;
          color: #666666;
          font-weight: bold;

          &:after {
            content: '/';
            margin: 0 3px;
          }

          &:last-child {
            &:after {
              content: '';
            }
          }
        }
      }

      .album {
        font-size: 14px;
        color: #333333;
        font-weight: bold;
        margin-left: 80px;
        flex: 1;
      }

      .duration {
        font-size: 14px;
        color: #333333;
        font-weight: bold;
        margin-left: 80px;
      }

      .tools {
        width: 25%;
        height: 100%;
        padding-left: 4.28%;
        display: flex;
        align-items: center;
        justify-content: space-between;
        opacity: 0;
        position: absolute;
        right: 4%;
        top: 0;

        i {
          font-size: 20px;
          cursor: pointer;
          display: block;
          color: #666666;
        }
      }
    }

    &.playing {
      p {
        color: $color-theme;
      }

      i {
        color: $color-theme;
      }

      .index-container {
        .num {
          display: none;
        }

        .play-icon {
          display: flex;
        }

        .play-btn {
          display: none;
        }
      }
    }

    &:hover {
      .index-container {
        .num {
          display: none;
        }

        .play-btn {
          display: block;
        }
      }

      &.playing {
        .index-container {
          .play-btn {
            display: none;
          }

          .play-icon {
            display: none;
          }

          .pause-btn {
            display: block;
          }
        }
      }
    }
  }
}
</style>